package com.example.juc;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Objects;

public class SingleTest {
    private static SingleTest singleTest;
    private SingleTest(){
        if(singleTest!=null){
            throw new RuntimeException("已经创建过了");
        }
        System.out.println(Thread.currentThread().getName());
    }
    public static SingleTest getGoods(){
        while (Objects.isNull(singleTest)){
            synchronized (SingleTest.class){
                if(singleTest==null){
                    singleTest=new SingleTest();
                }
            }
        }
        return singleTest;
    }
    public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        for (Integer i = 0; i < 10; i++) {
            new Thread(()->{
                SingleTest.getGoods();
            }).start();
        }
//        Constructor<? extends SingleTest> declaredConstructor = singleTest.getClass().getDeclaredConstructor(null);
//        declaredConstructor.setAccessible(true);
//        SingleTest singleTest = declaredConstructor.newInstance();
//        System.out.println(singleTest);
    }
}
